package com.ruoyi.web.zbAdmin.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.mongodb.client.result.UpdateResult;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.zbAdmin.domain.CaiSystemConfig;
import com.ruoyi.web.zbAdmin.domain.base.BasePo;
import com.ruoyi.web.zbAdmin.mapper.CaiSystemConfigMapper;
import com.ruoyi.web.zbAdmin.utils.QueryParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 招标系统配置
 * @author liandyao
 * @date 2022/12/14 15:18
 */
@Api(tags="才能招标-系统配置管理")
@RestController
@RequestMapping("/zbApi/systemConfig")
@Slf4j
public class CaiSystemConfigController {
    @Autowired
    private CaiSystemConfigMapper configMapper;

    public static final String collectionName = "cai_system_config";

    /**
     * 获取系统配置列表
     */
    @ApiOperation("获取系统配置列表")
    @GetMapping("/list")
    public TableDataInfo list(QueryParams userQuery) {

        log.info("=====>"+ JSONUtil.toJsonStr(userQuery));
        MongoTemplate mongoTemplate = this.configMapper.getMongoTemplate();

        Query query = new Query();
        // 设置起始页和每页查询条数
        Pageable pageable = PageRequest.of(userQuery.getCurPage(), userQuery.getPageSize(), Sort.by(Sort.Order.asc("_id")));
        // 查询记录总数
        long totalCount = mongoTemplate.count(query,collectionName);
        //查询分页后的记录
        List<CaiSystemConfig> result = mongoTemplate.find(query.with(pageable), CaiSystemConfig.class, collectionName);

        return TableDataInfo.success(result,totalCount);
    }


    @ApiOperation("获取系统配置详细") 
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable String id) {
        MongoTemplate mongoTemplate = this.configMapper.getMongoTemplate();
        CaiSystemConfig user = mongoTemplate.findById(id,CaiSystemConfig.class,collectionName);
        if(user==null){
            return AjaxResult.error("不存在!");
        }
        return AjaxResult.success("成功",user);
    }

    @ApiOperation("新增系统配置")
    /*@ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "系统配置id", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "username", value = "系统配置名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "系统配置密码", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "mobile", value = "系统配置手机", dataType = "String", dataTypeClass = String.class)
    })*/
    @PostMapping("/save")
    public AjaxResult save(CaiSystemConfig user) {

        this.configMapper.insert(user);

        return AjaxResult.success("保存成功");
    }

    @ApiOperation("更新系统配置")
    @PostMapping("/update")
    public AjaxResult update(CaiSystemConfig vo, HttpServletRequest request) {
        //因为SpringBoot接收不到_id这样的参数,暂时这样处理
        String _id = request.getParameter("_id");
        vo.set_id(_id);
        log.info("===>"+JSONUtil.toJsonStr(vo));
        if (StringUtils.isNull(vo) || StringUtils.isNull(vo.get_id())) {
            return AjaxResult.success("ID为空");
        }
        Update update = new Update();
        update.set("name",vo.getName());
        update.set("sort",vo.getSort());
        update.set("value",vo.getValue());
        update.set("remark",vo.getRemark());
        update.set("updateTime", DateUtil.now());
        UpdateResult re = this.configMapper.getMongoTemplate().upsert(Query.query(Criteria.where("_id").is(vo.get_id())),update,collectionName);

        return AjaxResult.success("修改成功",re.getMatchedCount());



    }

    @ApiOperation("删除系统配置信息")
    @ApiImplicitParam(name = "userId", value = "系统配置ID",required = true)
    @PostMapping("/delete/{userId}")
    public AjaxResult delete(@PathVariable String userId) {

        UpdateResult result=this.configMapper.getMongoTemplate().upsert(Query.query(Criteria.where("_id").is(userId)), Update.update("delFlag",BasePo.IS_DEL_YES),collectionName);

        return AjaxResult.success("删除成功",result.getMatchedCount());

    }

}
